import $ from '../../utils/ajax.js';
import util from '../../utils/util.js';
const app = getApp();
Page({
    data: {
        counts: {},
        moneyInput: '',
        bankNum: '',
        accountBank: '',
        available: 0,
    },
    onLoad(options) {
        this.onSaveWithdrawal();
    },
    onAll(){
        this.setData({
            moneyInput: this.data.available
        })
    },
    onInput(e) {
        let prime = e.detail.value;
        if (Number(prime) > Number(this.data.available)) {
            prime = this.data.available;
        }
        this.setData({
            moneyInput: prime
        }, () => {
            return prime;
        })
    },
    onSaveWithdrawal(){
        /**
         * 可体现金额
         */
        $.p(app.wxApp.url + 'agentnew/withdrawalAvailable', {}, app.onStrart, (res) => {
            this.setData({
                available: res.data.available,
                bankNum: res.data.bankNum.substr(res.data.bankNum.length - 4),
                accountBank: res.data.accountBank,
            })
            app.onShop();
        }, (res) => {
            util.showToast(res.data)
        })
    },
    onSub(){
        console.log(this.data.moneyInput)
        if (util.nonEmpty(this.data.moneyInput, '请填写金额')){
            $.p(app.wxApp.url + 'agentnew/saveWithdrawal', {
                amount: this.data.moneyInput
            }, app.onStrart, (res) => {
                util.showToast('提现申请已提交,一秒后返回上一页');
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 1000)
            }, (res) => {
                let text;
                app.onShop();
                if (res.status === 1004) {
                    text = '亲，本月提现次数已用完\r\n请次月在来提现哟'
                } else if (res.status === 1005) {
                    text = '亲，已有提现订单了\r\n请勿重复操作哟'
                } else {
                    util.showToast(res.data);
                    return;
                }
                wx.showModal({
                    title: '温馨提示',
                    content: text,
                    showCancel: false,
                    confirmColor: '#388beb',
                    success(res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                })
            })
        }
    }
})